package hrms.service;

import java.sql.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import hrms.dao.AdminDao;
import hrms.dao.EmpDao;
import hrms.entity.Admin;
import hrms.entity.Emp;
import hrms.util.PageUtil;
import hrms.util.Result;

@Service("empService")
public class EmpServiceImpl implements EmpService{
	
	@Resource(name="adminDao")
	private AdminDao adminDao ;
	
	@Resource(name="empDao")
	private EmpDao dao;

	public Result<List<Emp>> findByPage(String page, HttpServletResponse res) {
		return null;
	}
	public Result<List<Emp>> findByPage(String pages ,HttpServletResponse res,String deptId) {
		String message = "Emp";
		message += deptId;
		return new PageUtil<Emp>().findByPage(pages, res, dao, message);
	}
	
	public Result<Map<String, Integer>> pageTotal(HttpServletRequest req){
		String message = "Emp";
		return PageUtil.pageTotal(req, message); 
	}
	
	public Result<Object> addEmp(String empName, String empSex,String empAge,String empJob,String empDept) {
		Result<Object> result = new Result<Object>();
		List<Emp> list = dao.findAll(1, dao.findRow());
		for(Emp d : list){
			if(d.getEmpName().equals(empName)){
				result.setStatus(2);
				return result;
			}
		}
		Emp e = new Emp();
		e.setAge(Integer.parseInt(empAge));
		e.setEmpName(empName);
		e.setGender(empSex);
		e.setHiredate(new Date(System.currentTimeMillis()));
		String deptId = dao.findDeptIdByName(empDept);
		if(deptId == null){
			result.setStatus(3);
			result.setMsg("部门不存在！");
			return result;
		}
		e.setdName(empDept);
		e.setDeptId(Integer.parseInt(deptId));
		e.setJob(empJob);
		
		int num = dao.saveEmp(e);
		if(num!=1){
			result.setStatus(1);
			result.setMsg("创建出错！");
		} else {
			result.setStatus(0);
			result.setMsg("创建成功！");
		}
		return result;
	}

	public Result<Object> deleteEmp(String empId, HttpServletRequest req) {
		//获取当前登录的用户
		Admin adminUser = (Admin)req.getSession().getAttribute("admin");
		Integer rollUser = adminUser.getRoll();
		System.out.println(adminUser);
		if(rollUser == 2 ){
			Result<Object> result = new Result<Object>();
			result.setStatus(2);
			result.setMsg("权限不够!");
			return result;
		}
		
		Result<Object> result = new Result<Object>();
		int id = Integer.parseInt(empId);
		Emp e = dao.findByEmpId(id);
		if("经理".equals(e.getJob()) && rollUser != 0){
			result.setStatus(2);
			result.setMsg("权限不够!");
			return result;
		}
		
		int num = dao.deleteEmp(id);
		
		Cookie[] c = req.getCookies();
		String page = "1";
		for(Cookie cc : c){
			if(cc.getName().equals("EmpPage")){
				page = cc.getValue();
			}
		}
		int pageNum = Integer.parseInt(page);
		if(num != 1){
			result.setStatus(1);
			result.setMsg("删除出错");
		} else {
			result.setStatus(0);
			result.setMsg("删除成功");
			result.setData(pageNum);
		}
		return result;
	}

	public Result<Emp> findEmp(String empId, HttpServletRequest req) {
		Emp e = dao.findByEmpId(Integer.parseInt(empId));
		Result<Emp> r = new Result<Emp>();
		if(e != null){
			r.setData(e);
			r.setMsg("");
			r.setStatus(0);
			return r;
		}
		else{
			r.setStatus(1);
			r.setMsg("未找到员工信息！");
			return null;
		}
		
		
	}

	public Result<Object> uppdateEmp(String empId, String empName, String empSex, String empAge, String empJob,
			String empDept,HttpServletRequest req) {
			Result<Object> result = new Result<Object>();
			//获取当前登录的用户
			Admin adminUser = (Admin)req.getSession().getAttribute("admin");
			Integer rollUser = adminUser.getRoll();
			//获取将要被删除的部门号
			
			if(rollUser == 2 ){
				result.setStatus(2);
				result.setMsg("权限不够!");
				return result;
			}
			dao.deleteEmp(Integer.parseInt(empId));
			Emp e = new Emp();
			e.setEmpId(Integer.parseInt(empId));
			e.setAge(Integer.parseInt(empAge));
			e.setEmpName(empName);
			e.setGender(empSex);
			e.setHiredate(new Date(System.currentTimeMillis()));
			String deptId = dao.findDeptIdByName(empDept);
			if(deptId == null){
				result.setStatus(3);
				result.setMsg("部门不存在！");
				return result;
			}
			e.setdName(empDept);
			e.setDeptId(Integer.parseInt(deptId));
			e.setJob(empJob);
			
			int num = dao.updateEmp(e);
			if(num!=1){
				result.setStatus(1);
				result.setMsg("修改出错！");
			} else {
				result.setStatus(0);
				result.setMsg("修改成功！");
			}
		return result;
	}
	public Result<List<Emp>> search(String name) {
		Result<List<Emp>> result = new Result<List<Emp>>();
	    List<Emp> list = dao.findSearch("%"+name+"%");
		result.setData(list);
		result.setStatus(0);
		return result;
	}



}